package com.czl.module_main.viewmodel

import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.HouseKeeperListBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriber200Helper
import com.czl.base.util.RxThreadHelper

class HouseKeepersViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model)  {

    var searchName=""
    var currentPage = 0
    val loadCompleteEvent: SingleLiveEvent<HouseKeeperListBean> = SingleLiveEvent()

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getHouseKeepers()
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 0
        getHouseKeepers()
    })

    private fun getHouseKeepers() {
        model.getHouseKeepers(mapOf("areaNo" to model.getAreaId(),"name" to searchName,
            "pageNum" to (currentPage+1).toString(),
            "pageSize" to "10"))
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriber200Helper<BaseBean<HouseKeeperListBean>>() {
                override fun onResult(t: BaseBean<HouseKeeperListBean>) {
                        currentPage++
                        loadCompleteEvent.postValue(t.data)
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    loadCompleteEvent.postValue(null)
                }

            })
    }
    private fun webGetHouseKeepers(){
        model.webGetHouseKeepers(mapOf("areaNo" to model.getAreaId(),"name" to searchName,
            "pageNum" to (currentPage+1).toString(),
            "pageSize" to "10"))
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriber200Helper<BaseBean<HouseKeeperListBean>>() {
                override fun onResult(t: BaseBean<HouseKeeperListBean>) {
                    currentPage++
                    loadCompleteEvent.postValue(t.data)
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    loadCompleteEvent.postValue(null)
                }

            })
    }
}